<?php

use think\Request;

function check_condition($condition) {
    return !!preg_match('/^(\S+--\S+--\S+--[\S ]+__)*(\S+--\S+--\S+--[\S ]+)$/', $condition);
}

function str2where($condition) {
    if (check_condition($condition)) {
        $condition = preg_replace('/^(and|or)/i', '', $condition);
        $condition = str_replace(array('--','__'), array(' ',"' "), $condition);
        $old_char = [' ne ',' eq ',' lt ',' gt ',' le ',' ge ',' ct ',' nct '];
        $new_char = [" != '"," = '"," < '"," > '"," <= '"," >= '", " like '%"," not like '%"];
        $condition = str_replace($old_char, $new_char, $condition);
        $condition = preg_replace("/\s+(like\s+'[^']+)('|$)/i", " $1%$2", $condition);
        if ($condition != '') {
            $condition .= "'";
        }
        return $condition;
    }
    return null;
}

function msubstr($str, $start = 0, $length, $charset = 'utf-8', $ext = '...') {
    if (function_exists('mb_substr')) {
        $slice = mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
    } else {
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join('', array_slice($match[0], $start, $length));
    }
    return strlen($str) > strlen($slice) ? $slice . $ext : $slice;
}

function parseName($name, $type = 0) {
    if ($type) {
        return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
    } else {
        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
    }
}

function startWith($str, $needle) {
    return strpos($str, $needle) === 0;
}

function endWith($str, $needle) {
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }
    return (substr($haystack, -$length) === $needle);
}

function op($manager_id, $action, $content, $customer_id, $type = '') {
    $logs = [
        'manager_id' => $manager_id,
        'action' => $action,
        'content' => $content,
        'ip' => Request::instance()->ip(),
        'url' => Request::instance()->baseUrl(),
        'time' => date('Y-m-d H:i:s'),
        'customer_id' => $customer_id,
        'type' => $type
    ];

    db('log_operation')->insert($logs);
}

function random($len = 6, $type = 'mix') {
    $len = intval($len);

    if ($len > 90) {
        $len = 90;
    }
    
    $str = '';
    switch ($type) {
        case 'int':
            $templet = '012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789';
            break;
        case 'lowchar':
            $templet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl';
            break;
        case 'upchar':
            $templet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL';
            break;
        case 'char':
            $templet = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyzamwz0379bhklqdklg482156smyew';
            break;
        default:
            $templet = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+-=[]{}:";<>,.?|';
            break;
    }
    $start = mt_rand(1, (90 - $len));
    $string = str_shuffle($templet);
    return substr($string, $start, $len);
}
